package com.jsz.peini.controller.app;

import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.jsz.peini.bean.label.LabelUser;
import com.jsz.peini.common.constants.Constants;
import com.jsz.peini.common.util.PeiniUtils;
import com.jsz.peini.mapper.label.LabelUserMapper;
import com.jsz.peini.service.label.LabelInfoService;

@Controller
public class LabelInfoController {
	private static final Logger logger = LoggerFactory.getLogger(LabelInfoController.class);
	@Autowired
	private LabelInfoService labelInfoService;
	@Resource
	private LabelUserMapper labelUserMapper;
	
	/**
	 * 人物标签显示
	 * @param userId 用户tokenid
	 * @param labelUser
	 * @return
	 */
	@RequestMapping(value="/getLabelInfoList")
	@ResponseBody
	public Object getLabelInfoList(HttpServletRequest request,String userId){
		LinkedHashMap<String, Object> infoMap = new LinkedHashMap<String, Object>();
		try{
			JSONObject obj = PeiniUtils.validToken(userId);
			if(obj.getInteger("resultCode") != 1){
				return obj;
			}
			userId = obj.getString("userId");
			
			LabelUser labelUser = new LabelUser();
			labelUser.setUserId(userId);
			labelUser.setLabelState(0);
			List<LinkedHashMap<String,Object>> labelInfo=labelInfoService.selectLabelInfoByUserInfoId(labelUser);
			labelUser.setLabelState(1);
			List<LinkedHashMap<String,Object>> labelInfoshow=labelInfoService.selectLabelInfoByUserInfoId(labelUser);
			infoMap.put("labelInfoshow", labelInfoshow);
			infoMap.put("labelInfo", labelInfo);
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.FAIL);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		return infoMap;
	}
	
	/**
	 * 修改标签状态
	 * @param request
	 * @param labelInfo 标签di数组
	 * @param userId 用户tokenid
	 * @return
	 */
	@RequestMapping(value="/setLabelInfoBylabelState")
	@ResponseBody
	public Object setLabelInfoBylabelState(HttpServletRequest request,String labelInfo,String userId){
		LinkedHashMap<String, Object> infoMap = new LinkedHashMap<String, Object>();
		try{
			JSONObject obj = PeiniUtils.validToken(userId);
			if(obj.getInteger("resultCode") != 1){
				return obj;
			}
			userId = obj.getString("userId");
			labelInfoService.setLabelInfoBylabelState(labelInfo,userId);
			infoMap.put("resultCode", Constants.SUCCESS);
			infoMap.put("resultDesc", Constants.SUCCESS_DESC);
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.EXCEPTION);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		return infoMap;
	}

}
